package ocean

import (
	"cid-media-api/internal/adcore"
	"context"
	"fmt"
	. "github.com/oceanengine/ad_open_sdk_go/models"
	"golang.org/x/time/rate"
)

type PromotionService struct {
	client      *OceanClient
	rateLimiter *rate.Limiter
}

func NewPromotionService(client *OceanClient, limiter *rate.Limiter) *PromotionService {
	return &PromotionService{
		client:      client,
		rateLimiter: limiter,
	}
}

func (s *PromotionService) BatchCreate(
	ctx context.Context,
	ad adcore.AdParams,
) (PromotionID *int64, err error) {

	// 等待直到我们可以发送请求
	if err := s.rateLimiter.Wait(ctx); err != nil {
		return nil, err
	}

	// 构建广告创建请求
	request := PromotionCreateV30Request{
		Name:      ad.Name,
		ProjectId: ad.CampaignID,
		// 其他字段需要根据实际的 adcore.AdParams 结构进行填充
	}

	// 发送广告创建请求
	resp, _, err := s.client.cli.PromotionCreateV30Api().
		Post(ctx).
		AccessToken("ACCESS_TOKEN").
		PromotionCreateV30Request(request).
		Execute()
	if err != nil {
		return nil, err
	}

	if resp.Data != nil && resp.Data.PromotionId != nil {
		promotionID := *resp.Data.PromotionId
		return &promotionID, nil
	} else {
		return nil, fmt.Errorf("创建广告响应数据为 nil")
	}

}
